package com.tms.app.tms.utils;

import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hl on 2017/5/24.
 */

public class VerifyUtil {
    public static  final String EX_NAME="^[A-Za-z][A-Za-z1-9_-]+$";//字母开头 + 数字/字母/下划线
    public static  final String EX_EMAIL="[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}"; //邮箱
    public static  final String EX_PHONE="^1\\d{10}$";//
    public static  final String EX_URL="^((http|https)://)?([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";//URL
    public static  final String EX_ID_CARD_18="^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";//18位身份证号
    public static  final String EX_ID_CARD_15="^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$";//15位身份证号


    /*******
     * 获取中间数字
     * @param string
     * @return
     */
    public static String getMobileNumber(String string){
        if (string==null)
            return null;
        String df=null;
        String   regEx  =  "[0-9]";
        StringBuffer stringBuffer=new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            if (Pattern.matches(regEx,string.substring(i,i+1)))
            {
                stringBuffer.append(string.substring(i,i+1));
            }
        }
        if (stringBuffer.length()>11)
            df=stringBuffer.substring(0,11).toString();
        return df;
    }
    /****
     *  验证文本控件值 正确性
     * @param views
     * @param strings  错误提示
     * @param tagView  提示tag
     * @return
     */
    public static boolean checkTextValueRight_Name(TextView[] views,String[] strings,TextView ...tagView)
    {
        for (int i = 0; i < views.length; i++) {
            if (!checkTextValueRight(views[i],EX_NAME))
            {
                if (tagView.length>1)
                {
                    tagView[i].setText(strings[i]);
                }else {
                    tagView[0].setText(strings[i]);
                }
                return false;
            }
        }
        return true;
    }


    /****
     *  验证文本控件值 正确性
     * @param views
     * @param strings  错误提示
     * @param tagView  提示tag
     * @param regEx  正则
     * @return
     */
    public static  boolean checkTextValueRight(TextView[] views,String[] strings,String[] regEx,TextView ...tagView)
    {
        for (int i = 0; i < views.length; i++) {
            if (!checkTextValueRight(views[i],regEx[i]))
            {
                if (tagView.length>1)
                {
                    tagView[i].setText(strings[i]);
                }else {
                    tagView[0].setText(strings[i]);
                }
                return false;
            }
        }
        return true;
    }

    /****
     *  验证文本控件值 正确性
     * @param views
     * @param strings  错误提示
     * @param regEx  正则
     * @return
     */
    public static  String  checkTextValueRight(TextView[] views,String[] strings,String[] regEx)
    {
        for (int i = 0; i < views.length; i++) {
            if (!checkTextValueRight(views[i],regEx[i]))
            {
                return strings[i];
            }
        }
        return null;
    }

    /*******
     * value 是否正确
     * @param view
     * @param regEx  正则
     * @return
     */
    public  static boolean checkTextValueRight(TextView view,String regEx){
            String value=view.getText().toString();
        if (value==null||value.length()<1||value.equals(""))
        {
            return false;
        }
        if (regEx!=null)
        {
            return checkValueMatch(value,regEx,false);
        }
        return true;
    }


    /*************
     * 正则匹配判断
     * @param value  要匹配的字符串
     * @param resEx  匹配规则
     * @param flag   忽略大小写
     * @return
     */
    public static boolean checkValueMatch(String value,String resEx,boolean flag){
        Pattern pattern = Pattern.compile(resEx);  // 编译正则表达式
        if (flag)
            pattern = Pattern.compile(resEx, Pattern.CASE_INSENSITIVE); // 忽略大小写的写法
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    public static boolean checkEmptiy(TextView view){
        if (view.getText()==null||view.getText().length()<1||view.getText().toString().replace(" ","").length()<1)
        {
            return false;
        }
        return true;
    }

}
